package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理controller",tags = "频道管理controller")
public class AdChannelcontroller {

    @Autowired
    AdChannelService adChannelService;

    //频道分页查询所有
    @ApiOperation(value = "根据分页条件查询所有频道",notes = "按照name模糊查询，查询结果按照orderASC排序")
    @PostMapping("/list")
    public ResponseResult findChannelByPage(@RequestBody ChannelDTO dto){
        return adChannelService.findChannelByPage(dto);
    }
    //频道新增
    @ApiOperation(value = "添加频道",notes = "添加新频道")
    @PostMapping("/save")
    public ResponseResult addAdChannel(@RequestBody AdChannel adChannel){
        return adChannelService.insertAdChannel(adChannel);
    }
    /*
    *
    * 频道修改，频道启用或禁用
    * */
    @ApiOperation(value = "修改频道")
    @PostMapping("/update")
    public ResponseResult updateAdChannel(@RequestBody AdChannel adChannel){
        return adChannelService.updateAdChannel(adChannel);
    }
    /*
    * 频道删除
    * */
    @ApiOperation(value = "频道删除")
    @GetMapping("/del/{id}")
    public ResponseResult deleteAdChannel(@PathVariable("id") Integer id){
        /*if(true){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }*/
        return adChannelService.deleteAdChannel(id);
    }
   /*
   * 查询所有频道
   * */
   @ApiOperation("查询所有频道")
    @GetMapping("/channels")
    public ResponseResult<AdChannel> findAll(){
       List<AdChannel> list = adChannelService.list();
       return ResponseResult.okResult(list);
   }

   /*
   * 根据id查询频道信息
   * */
   @ApiOperation("根据id查询频道信息")
   @GetMapping("/one/{id}")
   public ResponseResult<AdChannel> findOne(@PathVariable Integer id) {
       return ResponseResult.okResult(adChannelService.getById(id));
   }


}
